package com.ply.flashsalessystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ply.flashsalessystem.annotation.AuthorityName;
import com.ply.flashsalessystem.annotation.UserAuthority;
import com.ply.flashsalessystem.entity.pojo.OrderForGoods;
import com.ply.flashsalessystem.entity.pojo.ReturnOfGoods;
import com.ply.flashsalessystem.entity.pojo.ReturnOfGoodsDetails;
import com.ply.flashsalessystem.entity.result.Result;
import com.ply.flashsalessystem.entity.status.ReturnOrderDetailsStatus;
import com.ply.flashsalessystem.entity.status.ReturnOrderStatus;
import com.ply.flashsalessystem.entity.vo.user.ReturnOrderVo;
import com.ply.flashsalessystem.entity.wrapper.TimeWrapper;
import com.ply.flashsalessystem.entity.wrapper.WrapperOrder;
import com.ply.flashsalessystem.service.OrderForGoodsService;
import com.ply.flashsalessystem.service.ReturnOfGoodsDetailsService;
import com.ply.flashsalessystem.service.ReturnOfGoodsService;
import com.ply.flashsalessystem.service.UserService;
import com.ply.flashsalessystem.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XiaoPan
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/returnOrder")
@Api("退货 接口")
@CrossOrigin
public class ReturnOfGoodsController {
    @Autowired
    UserService userService;

    @Autowired
    ReturnOfGoodsDetailsService returnOfGoodsDetailsService;

    @Autowired
    ReturnOfGoodsService returnOfGoodsService;

    @Autowired
    OrderForGoodsService orderForGoodsService;

//    @AuthorityName(authorityId = 31, authorityName = "用户 申请退货")
    @UserAuthority
    @ApiOperation("申请退货")
    @PostMapping("user/requestReturnOrder/{orgId}")
    @Transactional
    public Result requestReturnOrder(@PathVariable("orgId") Integer orgId,
                                     @RequestBody List<ReturnOfGoodsDetails> list) {
        return userService.requestReturnOrderMany(orgId, list)
                ? Result.ok()
                : Result.error();
    }

    @AuthorityName(authorityId = 32, authorityName = "管理员 同意申请退货")
    @ApiOperation("同意 申请退货 参数: 退单详情id")
    @PostMapping("admin/agreeReturnOrder/{rogDId}")
    public Result agreeReturnOrder(@PathVariable("rogDId") Integer rogDId) {
        //判断 rogDId 是不是存在 并且  在service 中 判断
        return returnOfGoodsDetailsService.agreeReturnOrder(rogDId)
                ? Result.ok()
                : Result.error();
    }

    @AuthorityName(authorityId = 33, authorityName = "管理员 不同意申请退货")
    @ApiOperation("不同意 申请退货 参数: 退单详情id")
    @PostMapping("admin/disagreeReturnOrder/{rogDId}")
    public Result disagreeReturnOrder(@PathVariable("rogDId") Integer rogDId) {
        //判断 rogDId 是不是存在 并且  在service 中 判断
        return returnOfGoodsDetailsService.disagreeReturnOrder(rogDId)
                ? Result.ok()
                : Result.error();
    }

    //查询 退单 列表
//    @AuthorityName(authorityId = 34, authorityName = "用户 查询退单详情")
    @UserAuthority
    @ApiOperation("用户查询 退单")
    @PostMapping(value = {"user/queryReOrder/{current}/{limit}", "user/queryReOrder"})
    public Result queryReOrder(@PathVariable(value = "current", required = false) Integer current,
                               @PathVariable(value = "limit", required = false) Integer limit,
                               @RequestBody(required = false) TimeWrapper timeWrapper) {
        if (current == null || limit == null) {
            current = 0;
            limit = 10;
        }
        if (timeWrapper == null) {
            timeWrapper = new TimeWrapper();
        }
        WrapperOrder wrapperOrder = new WrapperOrder();
        wrapperOrder.setCreateTimeMax(timeWrapper.getCreateTimeMax());
        wrapperOrder.setCreateTimeMin(timeWrapper.getCreateTimeMin());

        Page<ReturnOrderVo> page = returnOfGoodsDetailsService.queryReturn(current, limit, wrapperOrder);

        List<ReturnOrderVo> records = page.getRecords();
        List<ReturnOrderVo> returnOrderVos = ReturnOrderStatus.addStatusString(records);

        return Result.ok()
                .message("查询成功")
                .data("total", page.getTotal())
                .data("rows", returnOrderVos);
    }

    @AuthorityName(authorityId = 35, authorityName = "管理员 查询退单详情")
    @ApiOperation("管理员 查询退单")
    @PostMapping(value = {"admin/queryReOrder/{current}/{limit}", "admin/queryReOrder"})
    public Result adminQueryReOrder(@PathVariable(value = "current", required = false) Integer current,
                                    @PathVariable(value = "limit", required = false) Integer limit,
                                    @RequestBody(required = false) WrapperOrder wrapperOrder) {
        if (current == null || limit == null) {
            current = 0;
            limit = 10;
        }
        if (wrapperOrder == null) {
            wrapperOrder = new WrapperOrder();
        }
        Page<ReturnOrderVo> page = returnOfGoodsDetailsService.queryReturn(current, limit, wrapperOrder);
        List<ReturnOrderVo> records = page.getRecords();
        List<ReturnOrderVo> returnOrderVos = ReturnOrderDetailsStatus.addStatusString(records);

        return Result.ok()
                .message("查询成功")
                .data("total", page.getTotal())
                .data("rows", returnOrderVos);
    }

    //    根据退单id查看退单详情
    @AuthorityName(authorityId = 36, authorityName = "管理员 根据退单id查看退单详情")
    @ApiOperation("管理员查询 根据退单id查看退单详情")
    @PostMapping(value = {"admin/queryReOrderDetailsByRodId/{current}/{limit}/{roId}", "admin/queryReOrderDetailsByRodId/{roId}"})
    public Result queryReOrderDetailsByRodId(@PathVariable(value = "current", required = false) Integer current,
                                             @PathVariable(value = "limit", required = false) Integer limit,
                                             @PathVariable("roId") Integer roId) {
        if (current == null || limit == null) {
            current = 0;
            limit = 10;
        }

        QueryWrapper<ReturnOrderVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rog_id", roId);

        //根据 退单id  查看 退单 详情
        Page<ReturnOrderVo> page = returnOfGoodsDetailsService.queryReOrderDetailsByRodId(current, limit, roId);
        List<ReturnOrderVo> records = page.getRecords();
        List<ReturnOrderVo> returnOrderVos = ReturnOrderDetailsStatus.addStatusString(records);
        return Result.ok()
                .message("查询成功")
                .data("total", page.getTotal())
                .data("rows", returnOrderVos);
    }

    //   用户 根据退单id查看退单详情
//    @AuthorityName(authorityId = 36, authorityName = "管理员 根据退单id查看退单详情")
//    @ApiOperation("管理员查询 根据退单id查看退单详情")
    @UserAuthority
    @PostMapping(value = {"/userQueryReOrderDetailsByRodId/{current}/{limit}/{roId}", "admin/userQueryReOrderDetailsByRodId/{roId}"})
    public Result userQueryReOrderDetailsByRodId(@PathVariable(value = "current", required = false) Integer current,
                                                 @PathVariable(value = "limit", required = false) Integer limit,
                                                 @PathVariable("roId") Integer roId) {
        if (current == null || limit == null) {
            current = 0;
            limit = 10;
        }
        // 判断 该 退单
        ReturnOfGoods byId = returnOfGoodsService.getById(roId);
        OrderForGoods byId1 = orderForGoodsService.getById(byId.getOrgId());
        if (!Objects.equals(byId1.getUserId(), UserUtils.getUserId())) {
            return Result.error().message("该退单id 不属于用户");
        }

        QueryWrapper<ReturnOrderVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rog_id", roId);

        //根据 退单id  查看 退单 详情
        Page<ReturnOrderVo> page = returnOfGoodsDetailsService.queryReOrderDetailsByRodId(current, limit, roId);
        List<ReturnOrderVo> records = page.getRecords();
        List<ReturnOrderVo> returnOrderVos = ReturnOrderStatus.addStatusString(records);
        return Result.ok()
                .message("查询成功")
                .data("total", page.getTotal())
                .data("rows", returnOrderVos);
    }


    // 用户 取消 退单id
    @UserAuthority
    @ApiOperation("用户 退单id 取消退单")
    @PostMapping("cancelReturnOrder/{roId}")
    @Transactional
    public Result cancelReturnOrder(@PathVariable("roId") Integer roId) {
        returnOfGoodsDetailsService.cancelReturnOrder(roId);
        return Result.ok().message("取消成功");
    }
}

